package com.chaos.system.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.dto.result.data.BooleanResult;
import com.chaos.framework.model.dto.result.data.DataResult;
import com.chaos.framework.model.dto.result.list.ListResult;
import com.chaos.framework.model.dto.result.page.PageResult;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.ListHelper;
import com.chaos.framework.model.helper.MapHelper;
import com.chaos.framework.starter.base.BaseController;
import com.chaos.system.api.data.RbacMenuData;
import com.chaos.system.dto.RbacMenuTreeDto;
import com.chaos.system.entity.RbacMenu;
import com.chaos.system.service.RbacMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@RestController
@Api(tags = "")
@RequestMapping("/rbacMenu")
public class RbacMenuTreeController extends BaseController<RbacMenuTreeDto> {
    @Resource
    private RbacMenuService rbacMenuService;

    @PostMapping("/tree/list.admin")
    @ApiOperation(value = "menuTree", httpMethod = "POST")
    public ListResult<RbacMenuTreeDto> menuTree() {
        // 查询所有菜单
        List<RbacMenu> allMenus = rbacMenuService.list();

        // 构建菜单ID到菜单对象的映射，便于快速查找
        Map<Long, RbacMenu> menuMap = allMenus.stream()
                .collect(Collectors.toMap(RbacMenu::getId, menu -> menu));

        // 构建菜单树
        List<RbacMenuTreeDto> menuTree = buildMenuTree(allMenus, menuMap);

        return listResult(menuTree);
    }

    private List<RbacMenuTreeDto> buildMenuTree(List<RbacMenu> allMenus, Map<Long, RbacMenu> menuMap) {
        // 找出根菜单（父ID为空的菜单）
        List<RbacMenuTreeDto> rootMenus = allMenus.stream()
                .filter(menu -> menu.getParentId() == null)
                .sorted(Comparator.comparingInt(RbacMenu::getSort))
                .map(menu -> convertToDto(menu, allMenus, menuMap))
                .collect(Collectors.toList());

        return rootMenus;
    }

    private RbacMenuTreeDto convertToDto(RbacMenu menu, List<RbacMenu> allMenus, Map<Long, RbacMenu> menuMap) {
        RbacMenuTreeDto dto = RbacMenuTreeDto.builder()
                .title(menu.getCnName())
                .key(menu.getId())
                .icon(menu.getIcon())
                .build();

        // 递归查找子菜单
        List<RbacMenuTreeDto> children = allMenus.stream()
                .filter(child -> menu.getId().equals(child.getParentId()))
                .sorted(Comparator.comparingInt(RbacMenu::getSort))
                .map(child -> convertToDto(child, allMenus, menuMap))
                .collect(Collectors.toList());

        if (!children.isEmpty()) {
            dto.setChildren(children);
        }

        return dto;
    }
}
